﻿package Cain
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLRequest;
	/**
	 * ...
	 * @author Cain@2009 in ADCAST
	 * 陣列依序下載
	 * LoaderList(Array);
	 * Array is String for loader,s url
	 * 方法
	 * ok(n:int):Boolean  陣列n是否載完
	 * conload(n:int):Loader  傳回陣列中 n Loader
	 * 事件
	 * "1","2"......第n下載完
	 * "end" 全部下載完
	 */
	public class LoaderList extends EventDispatcher
	{
		private var loader_url:Array;
		private var loader_arr:Array;
		private var load_ok_arr:Array;
		private var url:URLRequest;
		private var now_load:int;
		private var num:int;
		public function LoaderList(_loader_list:Array)
		{
			loader_url = _loader_list;
			loader_arr = new Array();
			load_ok_arr = new Array();
			init();
		}
		
		private function init():void
		{
			num = loader_url.length;
			for (var i:int = 0; i < num; i++) 
			{
				var loader:Loader = new Loader();
				loader_arr.push(loader);
				load_ok_arr.push(false);
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE,load_init);
			}
			now_load = 0;
			url = new URLRequest(loader_url[0]);
			loader_arr[0].load(url);
		}
		
		private function load_init(e:Event):void 
		{
			load_ok_arr[now_load] = true;
			dispatchEvent(new Event(String(now_load)));
			now_load++;
			if (now_load<num)
			{
				url = new URLRequest(loader_url[now_load]);
				loader_arr[now_load].load(url);
			}else {
				dispatchEvent(new Event("end"));
			}
		}
		public function ok(n:int):Boolean 
		{
			return load_ok_arr[n];
		}
		public function conload(n:int):Loader 
		{
			return loader_arr[n];
		}
	}
	
}